<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {
  public function __construct(){
    parent::__construct();
    $this->load->model('User_model','m');//load model dan singkat dengan m
    $this->load->helper('url');
    $this->load->library('session');

  }

  //tampilkan form untuk login, dan simpan ke session user
  public function login(){
    $this->load->library('table');
    //untuk memudahkan membuat form
    $this->load->helper('form');

	//jika ini post
	if($this->input->post('login')){
		//var_dump($this->m); //object(User_model)#15 (1) { ["_table":protected]=> string(4) "user" }
		$user = $this->m->checkUserPassword($this->input->post('email'),$this->input->post('password'));
		//check username dan password
		//var_dump($this->input->post());
		//var_dump($user);
		//if user exist within our database
		if($user){
			$this->session->set_userdata('id_user', $user->id);
			$this->session->set_userdata('email', $user->email);
			$this->session->set_userdata('level', $user->level);
			$this->session->set_userdata('nama', $user->nama);
			
			if(9 == (int) $this->session->userdata('level')){
				//howdy Admin
				redirect('user');
			}else{
				redirect('user/home');
			}
		}
		//not a correct login, show the login form again :D
		
	}
    //buka form
    echo form_open('');//login ke sini saja
    $this->table->add_row('Email',':',form_input('email'));
    $this->table->add_row('Password',':',form_password('password'));

    $this->table->add_row(form_submit('login', 'Login'));
	
	//cetak tabel
    echo $this->table->generate();
    //tutup form
    echo form_close();
	
  }
  
  public function logout()
  {
    $this->session->sess_destroy();
    redirect('user/login');
  }
  
  public function home()
  {
	if($this->session->userdata('id_user')){
		//user telah login, tampilkan tugasnya:
		echo "User Control Panel for ". $this->session->userdata('nama');
		echo anchor('user/logout','Logout');
	}else{
		//belum login?
		redirect('user/login');
	}
  }
  
  public function index()
  {
    //load library table agar mudah membuat tabel
    $this->load->library('table');
	
	//user should login first if willing to see this page
	if(!$this->session->userdata('id_user')) redirect('user/login');

	//if not 9, not an admin :D
	if(8>$this->session->userdata('level')) 
		return redirect('user/home');

    $all_user = $this->m->all();//return array of object
    foreach($all_user as $user){
      $this->table->add_row(
        $user->id,
        $user->email,
        $user->nama,
        $user->level,
        anchor('user/view/'.$user->id,'Lihat'),
        anchor('user/edit/'.$user->id,'Edit')
      );
    }
	$this->table->add_row(anchor('user/add','Add'));
    echo $this->table->generate();
  }



  public function add()
  {
    //untuk memudahkan membuat form
    $this->load->helper('form');
    //load library table agar mudah membuat tabel
    $this->load->library('table');

	
	//user should login first if willing to see this page
	if(!$this->session->userdata('id_user')) redirect('user/login');

	//if not 9, not an admin :D
	if(8>$this->session->userdata('level')) 
		return redirect('user/home');
		
    //buka form
    echo form_open('user/save');

    $this->table->add_row('Nama',':',form_input('nama'));
    $this->table->add_row('Email',':',form_input('email'));
    $this->table->add_row('Password',':',form_input('password'));
    $this->table->add_row('Level',':',form_input('level'));


    //tombol tambah
    $this->table->add_row(form_submit('tambah', 'Tambah'));
    $this->table->add_row(anchor('user', 'Kembali'));
    echo $this->table->generate();

    //tutup form
    echo form_close();
  }

  //save our new user
  public function save()
  {
	
	//user should login first if willing to see this page
	if(!$this->session->userdata('id_user')) redirect('user/login');

	//if not 9, not an admin :D
	if(8>$this->session->userdata('level')) 
		return redirect('user/home');
		
    //save 
    $data['nama']     = $this->input->post('nama');
    $data['email']    = $this->input->post('email');
    $data['password'] = md5($this->input->post('password'));
    $data['level']    = $this->input->post('level');

    //simpan id autoincrement yang dihasilkan untuk ditampilkan jika sukses disimpan
    $id = $this->m->insert($data);
		redirect('user/view/'.$id);

  }

  public function view($id=0)
  {
	if(0 == $id) redirect('user');
	//user should login first if willing to see this page
	if(!$this->session->userdata('id_user')) redirect('user/login');

	//if not 9, not an admin :D
	//only show their own data
	if(9>$this->session->userdata('level')  && $this->session->userdata('id_user') != $id)
			redirect('user/view/'.$this->session->userdata('id_user'));

	$this->load->library('table');
	$o = $this->m->get($id);
	if(!$o->id) redirect('user');//no user with this id in our db
	$this->table->add_row('id',':', $o->id);
	$this->table->add_row('created_at',':', $o->created_at);
	$this->table->add_row('updated_at',':', $o->updated_at);
	$this->table->add_row('Nama',':',$o->nama);
	$this->table->add_row('Email',':',$o->email);
	$this->table->add_row('Level',':',$o->level);
	$this->table->add_row(anchor('user/edit/'.$id,'Edit'));
	echo $this->table->generate();
  }

  public function edit($id)
  {
	//user should login first if willing to see this page
	if(!$this->session->userdata('id_user')) redirect('user/login');

	//if not 9, not an admin :D
	if(8>$this->session->userdata('level')) 
		return redirect('user/home');
		
	$this->load->library('table');
    $this->load->helper('form');
    $o = $this->m->get($id);

    echo form_open('user/update/'.$o->id);
    //push the table
    $this->table->add_row('id',':', $o->id);
    $this->table->add_row('created_at',':', $o->created_at);
    $this->table->add_row('updated_at',':', $o->updated_at);

    $this->table->add_row('Nama',':',form_input('nama',$o->nama));
    $this->table->add_row('Email',':',form_input('email',$o->email));
    $this->table->add_row('Level',':',form_input('level',$o->level));
    $this->table->add_row('MD5 Password',':',form_input('password',$o->password));

    $this->table->add_row(form_submit('update', 'Update'));
    $this->table->add_row(anchor('user', 'Kembali'));

    //now render the table
    echo $this->table->generate();
    echo form_close();


  }

  public function update($id){
    //update
    $data['nama']=$this->input->post('nama');
    $data['email']=$this->input->post('email');
    $data['password'] = $this->input->post('password');
    $data['level']    = $this->input->post('level');
    $this->m->update($id,$data);
		redirect('user/view/'.$id);

  }

}
